import Vue from "vue";
import VueRouter from "vue-router";
import * as menuData from '@/api/menuData'
import {generateChildRouters} from '@/utils/util'
console.log('menuData:',menuData)
if (!window.VueRouter) {
  Vue.use(VueRouter);
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}


//懒加载
const Home = () => import('../views/Home.vue');
const About = () => import('../views/About.vue');
// const About = () => import('../views/About.vue');



const routes = [
  {
    path: "/",
    name: "Index",
    component: Home,
    meta: {
      keepAlive: true
    },
    children:[
      {
        path:'/userManagement/index',
        component:()=>import('../views/systemManagement/userManagement/index'),
        name:'userManagement',
        meta: {
          keepAlive: true
        },
      },
      {
        path:'/userManagement/test',
        component:()=>import('../views/systemManagement/test'),
        name:'userManagement-test',
        meta: {
          keepAlive: true
        },
      }
    ]
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: About,
    meta: {
      keepAlive: false
    }
  }
];
generateChildRouters(menuData.menu.result.menu) 
console.log('---------------routes:',routes)
const router = new VueRouter({
  mode:'history',
  routes
});
router.beforeEach((to, from, next) => {
  // 清空无用的页面缓存
  let toDepth = to.path.split('/').length
  let fromDepth = from.path.split('/').length
  if (toDepth < fromDepth) {
    // console.log('back...')
    from.meta.keepAlive = false
    to.meta.keepAlive = true
  }
  next()
})


export default router;